from overrides import override
from fastapi import Depends
import uuid
import typing as T
from datetime import datetime

from ....pojo.dto.UserSessionDTO import UserSessionDTO
from ....pojo.entity.User2Session import User2Session
from ..UserSessionService import UserSessionService as _UserSessionService
from ...mapper.UserSessionMapper import UserSessionMapper, _UserSessionMapper


class UserSessionServiceImpl(_UserSessionService):
    mapper: _UserSessionMapper = None

    def __call__(self, mapper: UserSessionMapper):
        self.mapper = mapper
        return self

    @override
    async def createSession(self, userSessionDTO: UserSessionDTO):
        createTime = datetime.now()
        alias = userSessionDTO.alias or f"{uuid.uuid4()}"
        entity = User2Session(
            userId=userSessionDTO.userId,
            alias=alias,
            createTime=createTime,
        )
        await self.mapper.createSession(entity)

    @override
    async def getSessionByUserId(self, userId: int):
        return await self.mapper.getSessionByUserId(userId)

    @override
    async def updateSessionNameBySessionId(self, sessionId: int, name: str):
        await self.mapper.updateSessionNameBySessionId(sessionId, name)

    @override
    async def deleteSessionBySessionId(self, sessionId: int):
        await self.mapper.deleteSessionById(sessionId)


UserSessionService = T.Annotated[
    UserSessionServiceImpl, Depends(UserSessionServiceImpl())
]
